//
//  File.swift
//  SwiftLearnTest
//
//  Created by 黄建华 on 2018/2/13.
//  Copyright © 2018年 黄建华. All rights reserved.
//
let SN_PublicClassSwift = SNPublicClass.sharedInstance()
let SN_KKSendClassSwift = KKBLEService.sharedInstance().send
let SN_KKBleConnet = KKBLEConnect.sharedInstance()

import UIKit
// MARK: - 尺寸信息
// 全屏宽度
let ScreenWitdh = UIScreen.main.bounds.width
// 全屏高度
let ScreenHeight = UIScreen.main.bounds.height
// tabbar切换视图控制器高度
let kTabbarHeight = 49.0
// 搜索视图高度
let kSearchBarHeight = 45.0
// 状态栏高度
let kStatusBarHeight    = 20.0
// 导航栏高度
let kNavigationHeight   = 44.0

// MARK: - 屏幕尺寸信息
//判断iPhone4
let IP4:Bool! = (UIScreen.main.bounds.size.height == 960) ? true : false
//判断iPhone5/5c/5s
let IP5:Bool! = (UIScreen.main.bounds.size.height == 1136) ? true : false
//判断iPhone6/6s
let IP6:Bool! = (UIScreen.main.bounds.size.height == 1334) ? true : false
//判断iPhone6p
let IP6P:Bool! = (UIScreen.main.bounds.size.height == 2208) ? true : false
//判断iPhone X
let IPX:Bool! = (UIScreen.main.bounds.size.height == 2436) ? true : false
// 当前app版本号
let GetAppCurrentVersion = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString")

func  ValueIXOrOther (ip:CGFloat,ix:CGFloat) ->CGFloat
{
    if (IPX)
    {
        return ix
    }
    else
    {
        return ip
    }
}

func ValueWithType(ip4:CGFloat,ip5:CGFloat,ip6:CGFloat,ip6p:CGFloat,ipx:CGFloat) -> CGFloat {
    if(IP4)
    {
        return ip4
    }
    else if(IP5)
    {
        return ip5
    }
    else if(IP6P)
    {
        return ip6
    }
    else if(IP6P)
    {
        return ip6p
    }
    else
    {
        return ipx
    }
}

// MARK: - 常用宏
func SMakeRect(x1:CGFloat,y1:CGFloat,width1:CGFloat,height1:CGFloat) ->CGRect
{
    let returnRect = CGRect(x:x1,y:y1,width:width1,height:height1)
    return returnRect
}

func  SmakePoint (x1:CGFloat,y1:CGFloat) ->CGPoint
{
    return CGPoint (x:x1,y:y1)
}

func SMakeSize (x:CGFloat,y:CGFloat) -> CGSize
{
    return CGSize(width:x, height:y)
}

func SMakeFont (x:CGFloat) -> UIFont
{
    return UIFont.systemFont(ofSize:x)
}

func UIColorFromRGB(rgbValue: UInt) -> UIColor
{
    return UIColor(
        red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
        green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,
        blue: CGFloat(rgbValue & 0x0000FF) / 255.0,
        alpha: CGFloat(1.0)
    )
}

func SMakeLabelAttributeString(labelText:String,rangeX:Int,rangeY:Int)->NSMutableAttributedString
{
    
    let attributeString = NSMutableAttributedString(string:"\(labelText)")
    attributeString.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor.black,
                                 range: NSMakeRange(rangeX, rangeY))
    return attributeString
}

// 根据图片名加载图片
func SmakeImageWithName (imageName:String) ->UIImage
{
    return UIImage(named:imageName)!
}

// 本地沙盒路径获取图片
func SmakeImageWithMainPath (forResource:String,type:String) ->UIImage
{
    let path = Bundle.main.path(forResource: forResource, ofType: type)
    let newImage = UIImage(contentsOfFile: path!)
    return newImage!
}

// 从网络或者URL拉取图片data数据
func SmakeImageWithUrl (urlString:String) ->UIImage
{
    let url = URL(string:urlString)
    //从网络获取数据流
    let data = try! Data(contentsOf: url!)
    //通过数据流初始化图片
    let newImage = UIImage(data: data)
    return newImage!
}

func SMakeText (key:String) ->String
{
    return NSLocalizedString(key, comment: "default")
}

// label
func  CreatUIlabel (frame:CGRect,text:String) ->UILabel
{
    let label = UILabel(frame:frame)
    label.adjustsFontSizeToFitWidth = true
    label.textColor = UIColor.black
    label.text = text
    label.font = SMakeFont(x: 17.0)
    label.textAlignment = .center
    return label
}

// button
func  CreatUIButton (frame:CGRect,text:String) ->UIButton
{
    let button = UIButton(frame:frame)
    button.setTitle(text, for:.normal)
    button.titleLabel?.font = SMakeFont(x: 17.0)
    button.setTitleColor(UIColor.white, for: .normal)
    button.setTitleColor(UIColor.gray, for: .highlighted)
    //        //设置按钮在特定状态下的图片显示
    //        button.setImage(UIImage(named: "back"), forState: .Normal)
    //设置按钮选中状态
    //        followButton.selected = true
    //设置是否响应用户touch事件
    //        followButton.userInteractionEnabled = true
    //        //设置按钮的背景颜色
    //        followButton.backgroundColor = UIColor.grayColor()
    //        //关闭按钮处于高亮状态图片变暗显示状态
    //        followButton.adjustsImageWhenHighlighted = false
    //        //设置按钮图片的内部填充
    //        followButton.imageEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10)
    //        //设置按钮文本的内部填充
    //        followButton.titleEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10)
    return button
}

// imageView
func CreatUIImageView (frame:CGRect,imageName:String) ->UIImageView
{
    let imageView = UIImageView(frame:frame)
    imageView.image = SmakeImageWithName(imageName: imageName)
    //设置图片视图高亮状态显示图片
    //        imgView.highlightedImage = UIImage(named: "exam1.png")
    //        /**
    //         *.ScaleToFill:缩放图片填满整个图片视图
    //         *.ScaleAspectFit:缩放内容以适应原图尺寸，没有铺满的地方保持透明
    //         *.ScaleAspectFill:按钮高亮显示状态
    //         **/
    //        imgView.contentMode = .ScaleAspectFit
    //        //设置图片视图是否高亮显示
    //        imgView.highlighted = true
    //        //设置是否响应用户touch事件,默认为false
    //        imgView.userInteractionEnabled = true
    //        //播放一组图片
    //        let img1 = UIImage(named: "play1.png")!
    //        let img2 = UIImage(named: "play2.png")!
    //        let img3 = UIImage(named: "play3.png")!
    //        let images = [img1, img2, img3]
    //        //设置需要播放的一组图片
    //        imgView.animationImages = images
    //        //设置播放持续时间
    //        imgView.animationDuration = 1
    //        //设置播放重复次数
    //        imgView.animationRepeatCount = 20
    //        //开始播放
    //        imgView.startAnimating()
    //        //停止播放
    //        imgView.stopAnimating()
    return imageView
}

func  CreatUITextField (frame:CGRect,textString:String) ->UITextField
{
    let  textField = UITextField(frame:frame)
    //设置文本编辑框显示文字
    textField.text = textString
    //设置文本编辑框占位文字
    textField.placeholder = "请输入文字"
    //设置文本编辑框的边框显示类型
    textField.borderStyle = .roundedRect
    //设置文本编辑框的文字字体
    textField.font = UIFont.systemFont(ofSize: 15)
    textField.textColor = UIColor.black
    //设置文本编辑框输入文本时首字母是否自动大写
    textField.autocapitalizationType = .none
    //设置文本编辑框清除按钮显示模式
    textField.clearButtonMode = .whileEditing
    //设置文本编辑框的文本对齐方式
    textField.textAlignment = .center
    //设置文本编辑框返回键显示类型
    textField.returnKeyType = .search
    //设置是否为安全文本输入状态
    textField.isSecureTextEntry = false
    //设置输入键盘类型
    textField.keyboardType = .default
    //设置文本编辑框的代理
    //唤起键盘，让文本编辑框成为第一响应者
    textField.becomeFirstResponder()
    return textField
}

//    //将要开始编辑代理方法,此方法会在键盘弹出之前调用
//    func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
//        return true
//    }
//    //已经开始编辑代理方法，此方法会在键盘弹出之后调用
//    func textFieldDidBeginEditing(textField: UITextField) {
//        print(textField.text)
//    }
//    //当return按钮被点击时调用
//    func textFieldShouldReturn(textField: UITextField) -> Bool {
//        //收起键盘，注销文本编辑框为第一响应者
//        textField.resignFirstResponder()
//        return true
//    }
//    //将要结束编辑代理方法，此方法会在收起键盘的时候调用
//    func textFieldShouldEndEditing(textField: UITextField) -> Bool {
//        return true
//    }

func CreatUiSwitch (frame:CGRect,max:Float) ->UISlider
{
    let sliderControl = UISlider(frame:frame)
    //设置滑动条控件最大值
    sliderControl.maximumValue = max
    //设置滑动条控件最小值
    sliderControl.minimumValue = 0
    //设置滑动条控件初始值
    sliderControl.value = 0.5*max
    //设置滑动条控件最大值方向颜色显示
    sliderControl.maximumTrackTintColor = UIColor.brown
    //设置滑动条控件最小值方向颜色显示
    sliderControl.minimumTrackTintColor = UIColor.red
    //设置滑动条控件拖动按钮颜色
    sliderControl.thumbTintColor = UIColor.blue
    //        //设置滑动条控件最大值方向图片显示：正常状态
    //        sliderControl.setMaximumTrackImage(UIImage(named: "slidermax.png"), for:.normal)
    //        //设置滑动条控件最小值方向图片显示：正常状态
    //        sliderControl.setMinimumTrackImage(UIImage(named: "slidermin.png"), for:.normal)
    //        //设置滑动条控件拖动按钮显示图片：正常状态
    //        sliderControl.setThumbImage(UIImage(named: "thumb.png"), for:.normal)
    //为滑动条控件添加：值改变触发事件
    return sliderControl
}

//UIActivityIndicatorView
func  CreatUIActivityIndicatorView (frame:CGRect) ->UIActivityIndicatorView
{
    let activityControl = UIActivityIndicatorView(frame:frame)
    //设置加载指示器视图的风格
    activityControl.activityIndicatorViewStyle = .gray
    //设置加载指示器视图的背景颜色
    activityControl.backgroundColor = .red
    //        //开始动画
    //        activityControl.startAnimating()
    //        //停止动画
    //        activityControl.stopAnimating()
    return activityControl
}

// UISegmentedControl
func  CreatUISegmentedControl (frame:CGRect,itums:NSArray) ->UISegmentedControl
{
    let segmentedControl = UISegmentedControl(items:itums as! [Any])
    segmentedControl.frame = frame
    //设置分段控件颜色
    segmentedControl.tintColor = .red
    //设置分段控件默认选中索引
    segmentedControl.selectedSegmentIndex = 0
    //为分段控件添加值改变响应事件
    return segmentedControl
}

//   9、警告视图控制器（UIAlertController）：(UIAlertView 与 UIActionSheet在iOS8.0之后就弃用了，推荐使用UIAlertController)

//CreatUIProgressView
func  CreatUIProgressView (frame:CGRect) ->UIProgressView
{
    let progressView = UIProgressView(progressViewStyle: .bar)
    progressView.frame = frame
    //设置默认进度
    progressView.progress = 0.0
    //设置进度条加载过的颜色
    progressView.progressTintColor = .red
    //设置进度条未加载过的颜色
    progressView.trackTintColor = .brown
    //        //设置进度条加载过的图片展示
    //        progressView.progressImage = UIImage(named: "progress.png")
    //        //设置进度条未加载过的图片展示
    //        progressView.trackImage = UIImage(named: "track.png")
    return progressView
}

func  CreatUIPageControl (frame:CGRect,count:Int) ->UIPageControl
{
    let pageControl = UIPageControl(frame:frame)
    //设置页面控件的背景颜色
    pageControl.backgroundColor = .brown
    //设置总页数
    pageControl.numberOfPages = count
    //设置当前页数
    pageControl.currentPage = 0
    //设置页面控件指示器颜色
    pageControl.pageIndicatorTintColor = .red
    //设置页面控件当前页面指示器颜色
    pageControl.currentPageIndicatorTintColor = .blue
    return pageControl
}

//#define SHOWMBProgressHUD(aTitle, aMsg, aImg, aDimBG, aDelay) [MBHUDHelper showMBProgressHUDTitle:aTitle msg:aMsg image:aImg dimBG:aDimBG delay:aDelay];
func ShowMBProgressHUD (title:String,time:Float)
{
    MBHUDHelper.showMBProgressHUDTitle(title, msg: nil, image: nil, dimBG: false, delay: time)
}


